package com.example.huayang.appdome.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.huayang.appdome.R;
import com.example.huayang.appdome.base.BaseActivity;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 主界面
 */
public class MainActivity extends BaseActivity implements View.OnClickListener{
    private TextView textReceive = null;
    private EditText textSend = null;
    private Button btnConnect = null;
    private Button btnSend = null;
    private static final String ServerIP = "192.168.1.66";
    private static final int ServerPort = 23;
    private Socket socket = null;
    private String strMessage;
    private boolean isConnect = false;
    private OutputStream outStream;
    private Handler myHandler = null;
    private ReceiveThread receiveThread = null;
    private boolean isReceive = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

    }


    private void initView(){
        textReceive = (TextView)findViewById(R.id.textViewReceive);
        textSend = (EditText)findViewById(R.id.editTextSend);
        btnConnect = (Button)findViewById(R.id.buttonConnect);
        btnSend = (Button)findViewById(R.id.buttonSend);

        btnConnect.setOnClickListener(this);
        btnSend.setOnClickListener(this);

        myHandler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                textReceive.append((msg.obj).toString());
            }
        };

    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.buttonConnect:
                // TODO Auto-generated method stub
                if (!isConnect){
                    new Thread(connectThread).start();
                }
                break;


        }
    }

    //连接到服务器的接口
    Runnable connectThread = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                //初始化Scoket，连接到服务器
                socket = new Socket(ServerIP, ServerPort);
                isConnect = true;
                //启动接收线程
                isReceive = true;
                receiveThread = new ReceiveThread(socket);
                receiveThread.start();

            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
        }
    };

    //接收线程
    private class ReceiveThread extends Thread{
        private InputStream inStream = null;

        private byte[] buffer;
        private String str = null;

        ReceiveThread(Socket socket){
            try {
                inStream = socket.getInputStream();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        @Override
        public void run(){
            while(isReceive){
                buffer = new byte[1024*16];
                try {
                    inStream.read(buffer);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    str = new String(buffer,"UTF-8").trim();
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Message msg = new Message();
                msg.obj = str;
                myHandler.sendMessage(msg);
            }
        }
    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        if(receiveThread != null){
            isReceive = false;
            receiveThread.interrupt();
        }
    }
}
